package com.itheima.springboot_08_ssmp3.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.springboot_08_ssmp3.domain.Book;
import com.itheima.springboot_08_ssmp3.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

//@RestController // == @Controller+@ResponseBody //表示这个类的方法都有返回值 注释掉,就不会被spring监管到了
@RequestMapping("/books") //访问/books就能访问到这个类
public class BookController {
    @Autowired
    private IBookService bookService;
    @GetMapping
    public List<Book> getAll(){ //
        return bookService.list();
    }
    @PostMapping //post发送方式,参数类型是json,raw  http://127.0.0.1:8081/books
    public Boolean save(@RequestBody Book book){ //RequestBody 请求体参数
        return bookService.save(book);
    }

    //{
    //    "type": "纸质版",
    //    "name": "水浒传",
    //    "description": "宋江2"
    //}

    @PutMapping
    public Boolean update(@RequestBody Book book){ //RequestBody 请求体参数
        return bookService.updateById(book);
    }
    @DeleteMapping("{id}")//路径参数 http://127.0.0.1/books/2
    public Boolean delete(@PathVariable Integer id){ // PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return bookService.removeById(id);
    }
    @GetMapping("{id}")//路径参数 http://127.0.0.1/books/3
    public Book getById(@PathVariable Integer id){// PathVariable 表示路径中的id位置的值,要赋值给这里面的形参
        return bookService.getById(id);
    }
    @GetMapping("{currentPage}/{pageSize}") //http://127.0.0.1:8081/books/3/2
    public IPage<Book> getPage(@PathVariable int currentPage, @PathVariable int pageSize){
        return bookService.getPage(currentPage,pageSize);
    }
}
